package com.bootcamp.servlet.cart;

/**
 * @program: wrok-1101
 * @author: Keyu Li
 * @create: 2021-11-05 19:56
 **/

import com.bootcamp.servlet.mvc.model.Product;
import com.bootcamp.servlet.util.AuthLoginUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Deque;
import java.util.List;
import java.util.Map;

@WebServlet(name = "ShowFootPrintServlet", value = "/ShowFootPrintServlet")
public class ShowFootPrintServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Map footprintMap = (Map) session.getAttribute("footprintMap");
        Deque<Integer> footprintDeque = (Deque)session.getAttribute("footprintDeque");
        PrintWriter writer = response.getWriter();
        // 如果历史记录为空，则返回购物页面
        String storepage = request.getContextPath() + "/ShopStoreServlet";
        if (footprintDeque==null || footprintMap==null || footprintDeque.size()==0) {
            writer.println("历史记录为空！正在跳转购物页面ing");
            response.setHeader("refresh", "1;url=" + storepage);
            return;
        }
        // 最上面一行，到 购物主页，或购物车
        AuthLoginUtils.authLoginStatus(request, writer);
        String cart = request.getContextPath() + "/ShowCartServlet";
        writer.println("<div><a href='" + storepage + "'>返回购物页面</a>&nbsp;&nbsp;&nbsp;<a href='" + cart + "'>购物车</a></div>");
        // 展示历史记录及访问时间
        List<Product> products = (List<Product>) getServletContext().getAttribute("products");
        writer.println("<div><table>");
        // 第一行
        writer.println("        <tr style=\"font-size: larger\">\n" +
                "            <td>品牌</td>\n" +
                "            <td>颜色</td>\n" +
                "            <td>类别</td>\n" +
                "            <td>价格</td>\n" +
                "            <td>描述</td>\n" +
                "            <td>详情</td>\n" +
                "            <td>上次浏览(5条)</td>\n" +
                "            <td></td>\n" +
                "        </tr>");
        for (Integer id : footprintDeque) {
            for (Product p : products) {
                if (p.getId().equals(id)){
                    // 展示商品
                    // 输出商品详情
                    String detailhref = request.getContextPath() + "/GoodsDetailServlet?id=" + p.getId();
                    String footprintDate = footprintMap.get(id).toString();
                    writer.println("<tr>\n" +
                            "                <td>" + p.getName() + "</td>\n" +
                            "                <td>" + p.getColor() + "</td>\n" +
                            "                <td>" + p.getCategory() + "</td>\n" +
                            "                <td>" + p.getPrice() + "</td>\n" +
                            "                <td>" + p.getDescription() + "</td>\n" +
                            "                <td><a href='" + detailhref + "'>详情</a></td>\n" +
                            "                <td>" + footprintDate + "</td>\n" +
                            "        </tr>");
                }
            }
        }
        writer.println("<div><table>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
